package com.moss.system.service.impl;

import com.moss.common.entity.SysRoleEntity;
import com.moss.common.entity.SysRoleMenuEntity;
import com.moss.system.mapper.RoleMenuMapper;
import com.moss.system.service.RoleMenuService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleMenuServiceImpl implements RoleMenuService {

    @Resource
    RoleMenuMapper roleMenuMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveRoleMenuBatch(SysRoleEntity role) {
        int row = 1;
        Long[] menuIds = role.getMenuIds();
        Long roleId = role.getRoleId();
        List<SysRoleMenuEntity> list = new ArrayList<>();
        for (Long menuId : menuIds) {
            SysRoleMenuEntity roleMenu = new SysRoleMenuEntity();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            list.add(roleMenu);
        }
        if (!list.isEmpty()){
            row = roleMenuMapper.insertBatch(list);
        }
        return row;
    }
}
